home *** CD-ROM | disk | FTP | other *** search
- Path: EU.net!sun4nl!ittpub!ittpub!nntp
- Newsgroups: comp.lang.c++
- Subject: Re: Static creator methods
- Message-ID: <1996Jan4.113320.1726@ittpub>
- From: wil@ittpub.nl (Wil Evers)
- Date: 4 Jan 96 11:33:19 WET
- References: <4ce6is$gpp@rdsunx.crd.ge.com>
- Distribution: world
- Nntp-Posting-Host: lintilla
-
- In article <4ce6is$gpp@rdsunx.crd.ge.com>
- kornfein@unconfigured.xvnews.domain (Mark Kornfein) writes:
- > I have some code that I inherited that uses static creator methods
- > (functions) to create new instances of a class. Anotherwords the "new
- > ClassConstructor" is called from a static method instead of directly.
- >
- > I assumed that this was done because the original coder did not
- > understand C++. Now I read a manual for a class library that
- > recommends always using these, but does not say why.
- >
- > What are the advantages/disadvantages of encapsulating the
- > instantiation of objects this way?
-
- There are quite a few idioms (e.g. envelope/letter) that assume instances
- of particular classes are always allocated on the heap. In such cases I
- usually put a few static creator functions in that class, and declare the
- constructors as `protected' instead of `public'. As a result, the only way
- to instantiate objects of that class is through one of the creator
- functions.
-
- An additional advantage of instatiating objects this way is that the
- creator function may return a pointer to an object of some class derived
- from the class in which the creator function is declared. This way,
- differences in behaviour can be implemented through polymorphism without
- the instantiating user having to know or worry about it.
-
- - Wil
-